Recommended R packages for plotting :

  • tmap: special-purpose package for thematic maps in R. [static, focus here]
  • mapview: quick, single-function to interactively view spatial data(frames). [interactive, demonstrated]
  • ggplot: most popular package for datavis in R, recent and growing support for maps. [static, not discussed here]
  • leaflet: flexible, general purpose package for interactive maps. [interactive, not discussed here]

Learn more:

tmap In an nutshell:

  • Quick ’n dirty thematic maps: Quick thematic map: qtm()
  • Build map in layers with lots and lots of control and options: tm_shape(), tm_fill(), tm_borders(), tm_lines(), tm_layout() etc.
  • Cherry-on-top: switch between interactive and static plotting with tmap_mode('view') and tmap_mode('plot').

Some resources on tmap:

library(here)
library(mapview)
library(sf)
library(tmap)
library(dplyr)
library(readxl)
library(scales) # for function percent_format()
# (1) load spatial data
raillines <- st_read(here('data/source/census_1851_raillines/1851EngWalesScotRail_Lines.shp'))

districts_spatial <- st_read(here('data/source/census_1851_districts/1851EngWalesRegistrationDistrict.shp')) %>%
  mutate(CEN1 = as.numeric(as.character(CEN1))) # make sure identifiers are the same type

# (2) load and add data-of-interest
districts_data <- read_excel(here('data/census1851_districts_count.xlsx'))
districts <- left_join(districts_spatial, districts_data, by = c('CEN1' = 'district_id'))
tm_shape(districts) +
  tm_borders()

tm_shape(districts) +
  tm_fill('pct_secondary')

tm_shape(districts) +
  tm_borders(col = 'white') +
  tm_fill('pct_secondary', title = 'Emploment secondary sector')

tm_shape(districts) +
  tm_borders(col = 'white') +
  tm_fill('pct_secondary', title = 'Emploment secondary sector') +
  tm_shape(raillines) +
  tm_lines(col = 'grey60')

nwestern <- districts %>%
  filter(R_DIV == 'NORTH WESTERN')
  

tm_shape(nwestern) +
  tm_fill(col = 'pct_secondary') +
  tm_text('district_name', size = .5)

Putting it all together

map_final <- tm_shape(districts) +
  tm_borders(col = 'white') +
  tm_fill('pct_secondary', title = 'Secondary sector\nemployment', legend.format = percent_format(accuracy = 1), legend.hist = TRUE) +
  tm_shape(raillines) +
  tm_lines(col = 'grey60') +
  tm_layout(main.title = 'Railways & secondary sector employment in 1851', legend.outside = TRUE, frame = FALSE) +
  tm_credits('Source: Cambridge Group for the\nHistory of Population and Social Structure', position = 'right', align = 'right')

map_final

# save final map
tmap_save(map_final, here('output/england_1851_map_final.png'), width = 1920, height = 1920)
## Map saved to /home/rstudio/projects/historical-maps-r/output/england_1851_map_final.png
## Resolution: 1920 by 1920 pixels
## Size: 6.4 by 6.4 inches (300 dpi)

Cherry-on-top: tmap interactive mode

tmap_mode("view")
tm_shape(nwestern) +
  tm_fill(col = 'pct_secondary') +
  tm_text('district_name')
tmap_mode("plot")
tmap_mode("view")
## tmap mode set to interactive viewing
map_final
## Credits not supported in view mode.
## Warning: The shape districts is invalid. See sf::st_is_valid
tmap_mode("plot")
## tmap mode set to plotting